package com.wx.activitys.admin.controller;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wx.activitys.drp.model.BClick;
import com.wx.activitys.drp.service.BClickService;

@Controller
@RequestMapping("/click")
public class ClickController {

	@Autowired
	private BClickService bClickService;
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String to(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return "admin/clickManager/list";
	}
	
	@RequestMapping(value = "/newPage", method = RequestMethod.POST)
	@ResponseBody
	public Object newPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> parameter = new HashMap<String, Object>();
		List<BClick> toltelSum =bClickService.findMonthOrallClick(parameter);
        String date = request.getParameter("date");
        if(StringUtils.isBlank(date)){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        	date=sdf.format(new Date());
        }
        parameter.put("yearmonth", date);
        List<BClick> monthSum=bClickService.findMonthOrallClick(parameter);
        
        for (BClick bClick : toltelSum) {
        	boolean flag = true;
			for (int i = 0; i < monthSum.size(); i++) {
				if(bClick.getName().equals(monthSum.get(i).getName())){
					flag=false;
					bClick.setClickNum(monthSum.get(i).getTotelNum());
				}
				if(i==monthSum.size()-1){
					if(flag){
						bClick.setClickNum(0);
					}
				}
			}
		}
		parameter.clear();
		parameter.put("toltelSum", toltelSum);
		return parameter;
	}
}
